package neves.android.etmg.tilemap.mapcreater;

import neves.android.etmg.map.MapCreater;
import neves.android.etmg.sprite.GameSprite;
import neves.android.etmg.sprite.SpriteType;
import neves.android.etmg.tilemap.TileMap;


public class TileMapCreater implements MapCreater {
	
	protected TileMap targetMap;
	

	
	/* (non-Javadoc)
	 * @see neves.android.etmg.MapCreater#CreateMap(neves.android.etmg.TileMap)
	 */
	public TileMap CreateMap(){
		
		targetMap = new TileMap(512, 512);
		
		targetMap.MakeARoom(20, 20, 20, 40);
		
		return targetMap;

	}
	
	
	protected int[] RandomARoomSize(int minVector, int maxVector){
		int[] roomSize = new int[2];
		
		if(minVector<maxVector){
			roomSize[0] = RandomNumber(minVector,maxVector);
			roomSize[1] = RandomNumber(minVector,maxVector);
		
		}else
		{
			roomSize = new int[]{5,5};
			
		}
		return roomSize;
	}
	protected int RandomNumber(int min,int max){
		return min + (int)(Math.random()*10000 % Math.abs(max-min));
	}
	
	// positiveFlag = false return negative int
	protected int RandomNumber(int min,int max,boolean positiveFlag){
		int result = Math.abs(RandomNumber(min,max));
		if(positiveFlag){
			return result;
		}else{
			return (0-result);
		}
	}
	
	protected int RandomNumberSigned(int min, int max){
		int signFlag = (int)(Math.random()*10 % 2);
		return RandomNumber(min,max,(signFlag==0));
	}
	
	protected int[] RandomPosInRoom(int[] roomData) {
		int px = RandomNumber(roomData[0], roomData[0] + roomData[2]);
		int py = RandomNumber(roomData[1], roomData[1] + roomData[3]);
		return new int[] { px, py };
	}

	protected void ConnectTwoRoom(int[] roomA,int[] roomB){

		int[][] roomConnector = new int[2][2];
		
		int[] rABounder = new int[]{roomA[0]-1,roomA[0]+roomA[2],roomA[1]-1,roomA[1]+roomA[3]};
		int[] rBBounder = new int[]{roomB[0]-1,roomB[0]+roomB[2],roomB[1]-1,roomB[1]+roomB[3]};
		
		
		do{
			roomConnector[0] = RandomPosInRoom(roomA);
		}
		while((roomConnector[0][0]==rBBounder[0])
			||(roomConnector[0][0]==rBBounder[1])
			||(roomConnector[0][1]==rBBounder[2])
			||(roomConnector[0][1]==rBBounder[3]));
	
		
		do{
			roomConnector[1] = RandomPosInRoom(roomB);
		}
		while((roomConnector[1][0]==rABounder[0])
				||(roomConnector[1][0]==rABounder[1])
				||(roomConnector[1][1]==rABounder[2])
				||(roomConnector[1][1]==rABounder[3]));
		
		//  two style to make connect (v then h or h then v)
		boolean isX_Y = ((Math.random()*10)%2 == 0 ? true:false);
		
		if(isX_Y){
			MakeTunnel(roomConnector[0][0],roomConnector[1][0],roomConnector[0][1],false); // hor line
			MakeTunnel(roomConnector[0][1],roomConnector[1][1],roomConnector[1][0],true);  // ver line
		}else{
			MakeTunnel(roomConnector[0][0],roomConnector[1][0],roomConnector[1][1],false); // hor line
			MakeTunnel(roomConnector[0][1],roomConnector[1][1],roomConnector[0][0],true);  // ver line
		}


			
		
	}
	
	

	protected boolean MakeTunnel( int tunnelpA, int tunnelpB, int tunnelFix, boolean isVerical){
		
		int pLength = Math.abs(tunnelpA - tunnelpB)+1;
		
		if(tunnelpA>tunnelpB){
			int temp = tunnelpA;
			tunnelpA = tunnelpB;
			tunnelpB = temp;
		}
		
		boolean issuccess = false;
		
		if(isVerical){
			issuccess = targetMap.MakeAPassage(tunnelFix,tunnelpA, pLength, isVerical);
		}else{
			issuccess = targetMap.MakeAPassage(tunnelpA,tunnelFix, pLength, isVerical);
		}
		
		return issuccess;
		
	}
		
	

	
	
}
